﻿using LivestreamE_Commerce.EntityFrameworkCore;
using LivestreamE_Commerce.IService;
using LivestreamE_Commerce.Model.RBAC;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LivestreamE_Commerce.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MyUserController : ControllerBase
    {
        LivestreamE_CommerceDbContext db;
        private readonly JWTTokenOptions _tokenOptions;
        public MyUserController(LivestreamE_CommerceDbContext _db,JWTTokenOptions tokenOptions)
        {
            db = _db;
            _tokenOptions = tokenOptions;
            
        }
        
        [HttpPost]
        [Route("Login")]
        public IActionResult Login(UserModel model)
        {
            if (model.LoginName == null && model.LoginPwd == null)
            {
                return Ok(new { state = false, msg = "用户或密码为空" });
            }
            var list = db.UserModel.Where(x => x.LoginName.Equals(model.LoginName) && x.LoginPwd.Equals(model.LoginPwd)).FirstOrDefault();
            if (list == null)
            {
                return Ok(new { state = false, msg = "用户或密码错误" });
            }
            else
            {
                return Ok(new { state = true, msg = "登录成功", list = list });
            }
        }
        [HttpPost, Route("upload")]
        public ActionResult Upload(IFormFile file)
        {
            string rootdir = AppContext.BaseDirectory.Split(@"\bin\")[0];
            string fname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + System.IO.Path.GetExtension(file.FileName);
            var path = rootdir + @"\Upload\" + fname;
            using (System.IO.FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Flush();//清空文件流
            }
            return Ok(new { newFileName = "http://localhost:8090/" + fname });//将能访问新文件的网址回传给前端
        }
        
    }
}
